
package edu.uoc.tds.pec4.client;

import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import edu.uoc.tds.pec4.client.gestion.ControladorConexion;
import edu.uoc.tds.pec4.i18n.TDSLanguageUtils;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.rmi.RemoteException;
import java.sql.SQLException;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import edu.uoc.tds.pec4.beans.Idioma;


public class PanelCambioIdiomaUI extends JFrame {

	/**
	 * @author asosal
	 *
	 */
	private static final long serialVersionUID = 7208938737041969200L;
	private JPanel contentPane;
	private int idUsuario;
	private String locale="ES";
	@SuppressWarnings("rawtypes")
	private JComboBox cmbIdioma = new JComboBox();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PanelCambioIdiomaUI frame = new PanelCambioIdiomaUI();
					frame.setVisible(true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("error.general"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PanelCambioIdiomaUI() {
		    
		/**Obtenemos la info de Usuario del menu Principal.*/
        setIdioma(PanelMenusUI.getIdioma());
        setIdUsuario(PanelMenusUI.getIdUsuario());
        
		TDSLanguageUtils.setDefaultLanguage("i18n/"+locale+"_messages");
		setTitle(TDSLanguageUtils.getMessage("lbl.titulo.cidioma"));
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		cmbIdioma.setModel(new DefaultComboBoxModel(Idioma.values()));
		cmbIdioma.setBounds(205, 91, 121, 23);
		contentPane.add(cmbIdioma);
		
		JButton btnAceptar = new JButton(TDSLanguageUtils.getMessage("btnAceptar"));
		btnAceptar.addMouseListener(new java.awt.event.MouseAdapter() 
        {
            public void mouseReleased(java.awt.event.MouseEvent evt)
            {
                 btnAceptarMouseReleased(evt);
            }

			private void btnAceptarMouseReleased(java.awt.event.MouseEvent evt) 
			{
				if(cmbIdioma.getSelectedIndex()==0)
				{
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Info.Idioma.indisponible"), TDSLanguageUtils.getMessage("cliente.info.titulo"), 1);
				}
				else
				{			
				String idioma="ES";
				try {
		        	switch (cmbIdioma.getSelectedIndex())
		        	{
		        	case 1: idioma="ES";break;
		        	case 2: idioma="IN";break;
		        	default:idioma="ES";
		        	}
		        	if(ControladorConexion.cambioIdioma(getIdUsuario(),idioma)==1)
		        	{
		        		JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Info.CambioIdioma"), TDSLanguageUtils.getMessage("cliente.info.titulo"), 1);
		        		cierra();
		        	}
		        	else JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("Error.CambioIdioma"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
				} catch (RemoteException e) {
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("cliente.error.conectar"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("mensaje.remoto.conexionFallida"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);					
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, TDSLanguageUtils.getMessage("error.general"), TDSLanguageUtils.getMessage("cliente.error.titulo"), 0);
				}
												
			}
		}
 });
		btnAceptar.setBounds(92, 176, 100, 23);
		contentPane.add(btnAceptar);
		
		JButton btnCancelar = new JButton(TDSLanguageUtils.getMessage("btnCancelar"));
		btnCancelar.addMouseListener(new java.awt.event.MouseAdapter() 
        {
            public void mouseReleased(java.awt.event.MouseEvent evt)
            {
                 btnCancelarMouseReleased(evt);
            }

			private void btnCancelarMouseReleased(java.awt.event.MouseEvent evt) 
			{
			cierra();									
			}
 });
		btnCancelar.setBounds(269, 176, 91, 23);
		contentPane.add(btnCancelar);
		
		JLabel lblSelect = new JLabel(TDSLanguageUtils.getMessage("lbl.CambioIdioma"));
		lblSelect.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblSelect.setBounds(57, 91, 136, 23);
		contentPane.add(lblSelect);		
		setIconImage (Toolkit.getDefaultToolkit().getImage(PanelCambioIdiomaUI.class.getResource("LogoTurva.gif")));
	}
	private void cierra(){
		this.dispose();
	}  
	public int getIdUsuario() {
		return idUsuario;
	}

	public void setIdUsuario(int uId) {
		this.idUsuario = uId;
	}

	public String getIdioma() {
		return locale;
	}

	public void setIdioma(String locale) {
		this.locale = locale;
	}
}
